package cn.wolfcode.luowowo.listener;

import cn.wolfcode.luowowo.cache.service.IStrategyStatisVORedisService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisVORedisService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.vo.TravelStatisVO;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.DateUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * redis数据初始化监听器
 */
@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Autowired
    private ITravelService travelService;

    @Autowired
    private ITravelStatisVORedisService travelStatisVORedisService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    //spring容器准备好之后马上执行的方法
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("----------vo对象的初始化--begin---------");

        //1.查询mongodb中所有攻略
        List<Strategy> list = strategyService.listAll();

        //2.遍历这个攻略对象封装攻略统计vo对象
        for (Strategy s : list) {
            //第一次初始化完成之后，如果页面进行操作，redis数据会发生变动，在没有持久化入库前，再次启动
            //如果不做跳过处理，会出现旧数据覆盖信息数据

            //如果redis已经存在vo，直接跳过
            if (strategyStatisVORedisService.isVoExists(s.getId())) {
                continue;//跳过本次循环
            }

            //3.添加到redis中
            StrategyStatisVO vo = new StrategyStatisVO();
            BeanUtils.copyProperties(s, vo);
            vo.setStrategyId(s.getId());
            strategyStatisVORedisService.setVO(vo);
        }

        List<UserInfo> users = userInfoService.listAll();

        for (UserInfo u : users) {

            if (userInfoRedisService.isUFExists(u.getId())) {
                continue;
            }

            String sidskey = RedisKeys.USER_SIDS.join(u.getId());
            String tidskey = RedisKeys.USER_TIDS.join(u.getId());

            redisTemplate.opsForValue().set(sidskey, JSON.toJSONString(u.getSids()));
            redisTemplate.opsForValue().set(tidskey, JSON.toJSONString(u.getTids()));
        }

        List<Travel> travels = travelService.listAll();
        for (Travel t : travels) {

            if (travelStatisVORedisService.isVoExists(t.getId())) {
                continue;
            }

            TravelStatisVO vo = new TravelStatisVO();
            vo.setTravelId(t.getId());
            BeanUtils.copyProperties(t, vo);
            travelStatisVORedisService.set(vo);
        }

        List<UserInfo> userInfos = userInfoService.listAll();
        for (UserInfo u : userInfos) {
            if (userInfoRedisService.isUTTV(u.getId())) {
                continue;
            }
            String ttyKey = RedisKeys.USER_TOTALVIEW.join(u.getId());
            String tdyKey = RedisKeys.USER_TODAYVIEW.join(u.getId());
            String uidKey = RedisKeys.USER_UIDS.join(u.getId());
            int totalView = u.getTotalView();
//            int todayView = u.getTodayView();
            List<String> uids = u.getUids();
            redisTemplate.opsForValue().set(ttyKey, String.valueOf(totalView));
            //当前时间
            Date now = new Date();
            //到今天的最后一秒
            Date end = DateUtil.getEndDate(now);
            //获取毫秒数
            long time = DateUtil.getDateBetween(now,end);
            redisTemplate.opsForValue().set(tdyKey, "0", time, TimeUnit.SECONDS);
            redisTemplate.opsForValue().set(uidKey, JSON.toJSONString(uids));
        }

        System.out.println("----------vo对象的初始化---end----------");
    }
}
